package com.jiyun.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiyun.model.*;
import com.jiyun.service.*;
import com.jiyun.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author 朱庆辉+
 * @Date 2023/7/28 16:05
 * @Email 1018276870@qq.com
 */
@RestController
@RequestMapping("clogin")

public class CloginController {
    @Autowired
    private CloginService cloginService;
    @PostMapping("login")
    public R login(@RequestBody User user){
        R ss = cloginService.login(user);
        return ss;
    }

    @GetMapping("getUid")
    public R getUid(){
        R uid = cloginService.getUid();
        return uid;
    }

    @GetMapping("findAllCourse")
    public R findAllCourse(){
        try {
            Integer num = cloginService.findAllCourse();
            return R.ok(num);
        }catch (Exception e){
            return R.error();
        }
    }

    @GetMapping("findAllTrain")
    public R findAllTrain(){
        try {
            Integer num = cloginService.findAllTrain();
            return R.ok(num);
        }catch (Exception e){
            return R.error();
        }
    }

    @GetMapping("findAllGame")
    public R findAllGame(){
        try {
            Integer num = cloginService.findAllGame();
            return R.ok(num);
        }catch (Exception e){
            return R.error();
        }
    }

    @Autowired
    private CourseService courseService;

    @GetMapping("findAllNum")
    public List<Upvote> findAllNum(){
        List<Upvote> numm = courseService.findAllNum();
        return numm;
    }


    @GetMapping("saveCourse")
    public String saveCourse(String name){
        String s = courseService.saveCourse(name);
        return s;
    }


    @GetMapping("findAllDate")
    public List<Game> findAllDate(){
        List<Game> allDate = cloginService.findAllDate();
        return allDate;
    }

    @GetMapping("findER")
    public List<String> findER(){
        ArrayList<String> list = new ArrayList<>();
        list.add("测试2");
        list.add("测试3");
        list.add("测试4");
        list.add("6");
        list.add("7");
        list.add("8");
        return list;
    }

    @PostMapping("findAllLiMit")
    public List<Course> findAllLiMit(@RequestBody CourseDto courseDto){
        List<Course> allLiMit = courseService.findAllLiMit(courseDto);
        return allLiMit;
    }





    @PostMapping("findAllTimie")
    public List<Course> findAllTimie(){
        List<Course> list = courseService.findAllTimie();
        return list;
    }

    @Autowired
    private LabelService labelService;


    @GetMapping("findByLabel")
    public List<Label> findByLabel(){
        return labelService.findByLabel();
    }

    @Autowired
    private RedisTemplate redisTemplate;
    @GetMapping("getToInfo")
    public void getToInfo(String name,Integer study,Integer gnum){
        Info info = new Info();
        info.setName(name);
        info.setGnum(gnum);
        info.setStudy(study);
        info.setCreatDate(new Date());
        redisTemplate.opsForValue().set(1,info);
    }

    @Autowired
    private GameService gameService;
    @GetMapping("setPoster")
    public void setPoster(@RequestParam(value = "name") String name){
        QueryWrapper<Game> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("game_name",name);
        Game game = gameService.getOne(queryWrapper);
        redisTemplate.opsForValue().set("setPoster", game);
    }

    @GetMapping("getPoster")
    public Game getPoster(){
        Game poster = (Game) redisTemplate.opsForValue().get("setPoster");
        return poster;
    }



    @GetMapping("setToInfo")
    public Info setToInfo(){
        Info o = (Info) redisTemplate.opsForValue().get(1);
        return o;
    }

    @GetMapping("labelAll")
    public List<LabelVo> labelAll(@RequestParam(value = "name") String name){
        String s = courseService.labelAll(name);
        String[] split = s.split(",");
        List<LabelVo> list = new ArrayList<>();
        for (String s1 : split) {
            LabelVo labelVo = new LabelVo();
            labelVo.setLabelName(s1);
            list.add(labelVo);
        }
        return list;
    }


    @Autowired
    private CatalogService catalogService;
    @GetMapping("findAllCatalog")
    public List<Catalog> findAllCatalog(CatalogDto catalogDto){
        List<Catalog> all = catalogService.findAll(catalogDto);
        return all;
    }

    @GetMapping("findAllGameList")
    public List<Game> findAllGameList(GameDto gameDto){
        QueryWrapper<Game> queryWrapper = new QueryWrapper<>();
        if (gameDto.getName()!=null){
            queryWrapper.like("game_name",gameDto.getName());
        }
        if (gameDto.getLabel()!=null){
            queryWrapper.like("label",gameDto.getLabel());
        }
        if (gameDto.getStatus()!=null){
            queryWrapper.eq("status",gameDto.getStatus());
        }
        List<Game> list = gameService.list(queryWrapper);
        return list;
    }

    @Autowired
    private TrainService trainService;

    @GetMapping("findAllTrainList")
    public List<Train> findAllTrainList(TrainDto trainDto){
        QueryWrapper<Train> queryWrapper = new QueryWrapper<>();
        if (trainDto.getTrainName()!=null){
            queryWrapper.like("train_name",trainDto.getTrainName());
        }
        if (trainDto.getLabel()!=null){
            queryWrapper.like("label",trainDto.getLabel());
        }
        if (trainDto.getStatus()!=null){
            queryWrapper.eq("status",trainDto.getStatus());
        }
        List<Train> list = trainService.findAll(trainDto);
        return list;
    }

    @GetMapping("findAllTeam")
    public List<Team> findAllTeam(){
        return cloginService.findAllTeam();
    }

    @GetMapping("saveUpdate")
    public R saveUpdate(String name){
        Game poster = (Game) redisTemplate.opsForValue().get("setPoster");
        if (poster.getTeamName()!=null){
            return R.error();
        }else {
            poster.setTeamName(name);
            gameService.saveOrUpdate(poster);
            return R.ok("报名成功");
        }
    }
    @Autowired
    private TestPaperService testPaperService;
    @RequestMapping("findAllTestPaper")
    public List<TestPaper> findAll(TestPaperDto testPaperDto){
        List<TestPaper> all = testPaperService.findAll(testPaperDto);
        return all;
    }

    @Autowired
    private ExamService examService;
    @RequestMapping("findAllByGameId")
    public Exam findAllByGameId(Integer id){
        QueryWrapper<Exam> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        Exam exam = examService.getOne(queryWrapper);
        return exam;
    }
    @Autowired
    private PaperGameService paperGameService;
    @RequestMapping("findAllPaper")
    public List<Exam> findAllPaper(String name){
        TestPaperDto testPaperDto = new TestPaperDto();
        List<TestPaper> paperList = testPaperService.findAll(testPaperDto);
        TestPaper testPaper = new TestPaper();
        for (TestPaper paper : paperList) {
            if (paper.getPaperName().equals(name)){
                testPaper.setId(paper.getId());
            }
        }
        QueryWrapper<PaperGame> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("paper_id",testPaper.getId());
        ArrayList<Exam> exams = new ArrayList<>();
        List<PaperGame> list = paperGameService.list(queryWrapper);
        for (PaperGame paperGame : list) {
            exams.add(examService.getById(paperGame.getId()));
        }
        return exams;
    }

    @PostMapping("savePaperGame")
    public void savePaperGame(Integer id,@RequestBody PaperGame paperGame){
        if (paperGame.getAnswer()!=null||paperGame.getAnswer()!=""){
            Integer ids = id - 1;
            QueryWrapper<PaperGame> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("exam_id",ids);
            PaperGame one = paperGameService.getOne(queryWrapper);
            one.setAnswer(paperGame.getAnswer());
            paperGameService.saveOrUpdate(one);
        }

    }

    @GetMapping("findGamePaper")
    public PaperGame findGamePaper(Integer id){
        QueryWrapper<PaperGame> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("exam_id",id);
        PaperGame one = paperGameService.getOne(queryWrapper);
        return one;
    }

    @GetMapping("setTrain")
    public void findById(Integer id){
        Train byId = trainService.findById(id);
        redisTemplate.opsForValue().set("setTrain", byId);
    }

    @GetMapping("getTrain")
    public Train getTrain(){
        Train setTrain = (Train) redisTemplate.opsForValue().get("setTrain");
        return setTrain;
    }
    @RequestMapping("findTrainGameId")
    public List<TestPaper> findTrainGameId(Integer id){
        List<TrainGame> trainGameId = trainService.findTrainGameId(id);
        List<TestPaper> list1 = new ArrayList<>();
        TestPaperDto testPaperDto = new TestPaperDto();
        List<TestPaper> all1 = testPaperService.findAll(testPaperDto);
        for (TrainGame trainGame : trainGameId) {
            for (TestPaper testPaper : all1) {
                if (trainGame.getGameId().equals(testPaper.getId())){
                    list1.add(testPaper);
                }
            }
        }
        return list1;
    }

    @RequestMapping("findAllHottest")
    public List<TestPaper> findAllHottest(TestPaperDto testPaperDto){
        return testPaperService.findAllHottest(testPaperDto);
    }

    @RequestMapping("setDoexercise")
    public void setDoexercise(Integer id){
        QueryWrapper<PaperGame> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("paper_id",id);
        List<PaperGame> list = paperGameService.list(queryWrapper);
        for (PaperGame paperGame : list) {
            paperGame.setAnswer("");
            paperGameService.saveOrUpdate(paperGame);
        }
        List<Exam> examArrayList = new ArrayList<>();
        for (PaperGame paperGame : list) {
            QueryWrapper<Exam> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("id",paperGame.getExamId());
            Exam one = examService.getOne(queryWrapper1);
            examArrayList.add(one);
        }
        redisTemplate.opsForValue().set("setDoexercise",examArrayList);
    }


    @RequestMapping("getDoexercise")
    public List<Exam> getDoexercise(){
        List<Exam> doexercise = (List<Exam>) redisTemplate.opsForValue().get("setDoexercise");
        return doexercise;
    }
}
